package com.jinyu.jyly.ui.mine.passageway;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.jinyu.jyly.R;
import com.jinyu.jyly.adapter.ExtensionAdapter;
import com.jinyu.jyly.adapter.PassageWayAdapter;
import com.jinyu.jyly.base.BaseFragment;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.GetPriceInfo;
import com.jinyu.jyly.bean.PassageWayInfo;
import com.jinyu.jyly.utils.AES;
import com.jinyu.jyly.view.GridSpacingItemDecoration;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * @author 62485
 */
public class PassageWayTabFragment extends BaseFragment<PassageWayPresenter> implements PassageWayContract.View {


    @BindView(R.id.btn_sub)
    Button btnSub;
    @BindView(R.id.rcv_price)
    RecyclerView rcv_price;
    @BindView(R.id.recyclerView_zd)
    RecyclerView recyclerView_zd;


    private PassageWayPresenter presenter;
    private int type;
    private ExtensionAdapter extensionAdapter;

    public static Set<Integer> positionSet = new HashSet<>();
    public Set<Integer> checkTYpeNameSet = new HashSet<>();
    private boolean selectMode = false; //选择模式 多选或者单选  true  多选
    private List<GetPriceInfo> mList;

    public static PassageWayTabFragment newInstance(int type) {
        PassageWayTabFragment newFragment = new PassageWayTabFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        newFragment.setArguments(bundle);
        return newFragment;
    }


    @Override
    protected View initView(ViewGroup container) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_tab_passage_way, container, false); //fragment_tab_passage_way
        return view;
    }

    @Override
    protected void loadData() {
        type = getArguments().getInt("type");
        initListener();
        presenter.get_greens();
//        presenter.getPrice("dd");

    }


    private void initListener() {
        rcv_price.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rcv_price.addItemDecoration(new GridSpacingItemDecoration(3, 10, false));
        recyclerView_zd.setLayoutManager(new LinearLayoutManager(getActivity()));
    }


    @Override
    public PassageWayPresenter getPresenter() {
        if (presenter == null) {
            presenter = new PassageWayPresenter(getActivity(), this);
        }
        return presenter;
    }


    @Override
    public void onAddGreen(AllInfoBean info) {
        switch (info.getStatus()) {
            case 200:
                String beanData = info.getData();
                String result = null;
                try {
                    result = AES.decrypt(beanData);
                    Gson gson = new Gson();
                    showToast("抢购成功");
                    getActivity().finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(info.getMsg());
                break;
            default:
                break;
        }
    }

    @Override
    public void onFailer(Throwable throwable) {

    }

    @Override
    public void onGetPrice(AllInfoBean infoBean) {

    }

    /**
     * 获取绿色通道
     * @param infoBean
     */
    @Override
    public void onGetGreens(AllInfoBean infoBean) {
        switch (infoBean.getStatus()) {
            case 200:
                String beanData = infoBean.getData();
                String result = null;
                try {
                    result = AES.decrypt(beanData);
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<PassageWayInfo>>() {}.getType();
                    List<PassageWayInfo> mList = gson.fromJson(result, listType);
                    if (mList != null && mList.size() != 0) {
                        PassageWayAdapter accountAdapter = new PassageWayAdapter(mList, R.layout.item_passage_way);
                        recyclerView_zd.setAdapter(accountAdapter);
                    }else{
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }

    }

    /**
     * 获取价格
     *
     * @param
     */
//    @Override
//    public void onGetPrice(AllInfoBean infoBean) {
//        switch (infoBean.getStatus()) {
//            case 200:
//                try {
//                    String result = AES.decrypt(infoBean.getData());
//                    Gson gson = new Gson();
//                    Type listType = new TypeToken<ArrayList<GetPriceInfo>>() {
//                    }.getType();
//                    mList = gson.fromJson(result, listType);
//                    if (mList != null && mList.size() != 0) {
//                        extensionAdapter = new ExtensionAdapter(getActivity(), mList);
//                        rcv_price.setAdapter(extensionAdapter);
//                        extensionAdapter.setOnItemClickListener(new ExtensionAdapter.OnTagItemClickListener() {
//                            @Override
//                            public void OnTagItemClick(View view, int position) {
//                                addOrRemove(position);
//                            }
//
//                            @Override
//                            public void OnTagItemLongClick(View view, int position) {
//                            }
//                        });
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                break;
//            case 422:
//                showToast(infoBean.getMsg());
//                break;
//            default:
//                break;
//        }
//    }

    @OnClick(R.id.btn_sub)
    public void onViewClicked() {
        setSignOutApp();
    }


    //抢拍
    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;
    private void setSignOutApp() {
        new QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle("竞拍提示")
                .setMessage("您确认花费20宝石交付1天商城竞拍保证金吗？")
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(0, "确定", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        presenter.addGreen();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }



    private void addOrRemove(int position) {
        if (positionSet.contains(position)) {
            // 如果包含，则撤销选择
            positionSet.remove(position);
            checkTYpeNameSet.remove(mList.get(position).getId());
        } else {
            // 如果不包含，则添加
            if (!selectMode) {
                //单选
                positionSet.clear();
                checkTYpeNameSet.clear();
                positionSet.add(position);
                checkTYpeNameSet.add(mList.get(position).getId());

            } else {
                positionSet.add(position);
                checkTYpeNameSet.add(mList.get(position).getId());
            }
        }
        if (positionSet.size() == 0) {
            // 如果没有选中任何的item，则退出多选模式
            extensionAdapter.notifyDataSetChanged();
            selectMode = false;
        } else {
            // 更新列表界面，否则无法显示已选的item
            extensionAdapter.notifyDataSetChanged();
        }
    }
}
